package suanfa;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zt
 * @date 2020-09-09 11:24
 */
public class SellTicket implements Runnable {
    private int tickets = 100;
    private Object obj = new Object();
    private Lock lock = new ReentrantLock();
    //在SellTicket类中重写run()方法实现卖票，代码步骤如下



    @Override
    public void run() {
        while (true) {
            lock.lock();
                //t1进来后，就会把这段代码给锁起来
                if (tickets > 0) {
                        Thread.sleep(100);
                        //t1休息100毫秒
                    System.out.println(Thread.currentThread().getName() + "正在出售第" + tickets + "张票");
                    tickets--; //tickets = 99;
                }
            }
            //t1出来了，这段代码的锁就被释放了
        }
    }
}
